package client;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;

import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import communication.ClientComm;

public class GraphicViewCenterPanel extends JPanel {
	private static final long serialVersionUID = 1L;
	
	private GraphicViewLeftPanel left;
	private GraphicViewRightPanel right;
	private ClientModel client;
	private ClientComm clientComm;
	private GraphicMap mappa;
	private JScrollPane scrollo;
	
	public GraphicViewCenterPanel(ClientModel cM, ClientComm cC, GraphicViewLeftPanel l, GraphicViewRightPanel r) {
		client = cM; left = l;
		clientComm = cC; right = r;
		
		setPreferredSize(new Dimension(GraphicView.centerPanelWidth, GraphicView.centerPanelHeight));
		setLayout(new FlowLayout(FlowLayout.CENTER));		
		
		setVisible(true);
		validate();
	}
	
	public JTextField[] loginRegCenterPanel() {		
		JPanel labels = new JPanel(new GridLayout(2,1,2,2));			
		JLabel us = new JLabel("Username");
		JLabel pass = new JLabel("Password");
		labels.add(us); labels.add(pass); 
		
		JPanel campi = new JPanel(new GridLayout(2,1,2,2));
		JTextField[] usPw = new JTextField[2];
		usPw[0] = new JTextField(null, 20);
		usPw[1] = new JPasswordField(null, 20);
		campi.add(usPw[0]); campi.add(usPw[1]);
		
		JPanel usPwPanel = new JPanel(new FlowLayout());
		usPwPanel.add(labels); usPwPanel.add(campi);
		add(usPwPanel);
		validate();
		return usPw;
	}
	
	public Object[] repaintNoRace() {
		removeAll();
		
		JPanel Specie = new JPanel(new FlowLayout());
		JLabel nomeLabel = new JLabel("Nome della tua specie");
		JTextField nomeSpecie = new JTextField(null, 20);
		Specie.add(nomeLabel); Specie.add(nomeSpecie);  
		
		CarnErbListener tipo = new CarnErbListener();
		JRadioButton carnivoro = new JRadioButton("Carnivoro");
		carnivoro.setActionCommand("c");
		carnivoro.addActionListener(tipo);
		JRadioButton erbivoro = new JRadioButton("Erbivoro");
		erbivoro.setActionCommand("e");
		erbivoro.addActionListener(tipo);
		
		ButtonGroup tipoSpecie = new ButtonGroup();
		tipoSpecie.add(carnivoro);
		tipoSpecie.add(erbivoro);
		
		JPanel tipoSpe = new JPanel(new FlowLayout());
		tipoSpe.add(carnivoro);
		tipoSpe.add(erbivoro);
		
		setBorder(new TitledBorder("Creazione nuova specie"));
		add(Specie); add(tipoSpe);
		repaint();
		validate();
		Object[] ret = new Object[2]; ret[0] = nomeSpecie; ret[1] = tipo; 
		return ret;
	}
	 //inizializza lo spazio per la mappa
	public void repaintAfterAccesso() {
		removeAll();
		if (mappa == null)
			mappa = new GraphicMap(client, clientComm, right, left);
		scrollo = new JScrollPane();
		scrollo.setPreferredSize(new Dimension(GraphicView.centerPanelWidth-20, GraphicView.centerPanelHeight-20));
		scrollo.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		scrollo.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		scrollo.setViewportView(mappa);
		add(scrollo);
		repaint();
		validate();
	}
	public void resetAfterLogout() {
		scrollo.setVisible(false);
		scrollo = null;
		removeAll();
		repaint();
		validate();		
	}
	// stampa la mappa
	public void paintMappaGenerale(String map) {
		if (mappa == null)
			mappa = new GraphicMap(client, clientComm, right, left);
		mappa.paintMappaGenerale(map);
		repaint();
		validate();
	}
	public String[] paintVistaLocale(String map) {
		String dinos[] = mappa.paintVistaLocale(map);
		repaint();
		validate();
		return dinos;
	}
	public void centraPrimoDino() {
		Coordinate c = ((Dinosauro)client.getGameStatus().myDinos.toArray()[0]).getCoord();
		int x = c.getX()*GraphicView.dimCasella - (scrollo.getWidth())/2; //posX del dino in pixel con origine in alto a dx - semiampiezza dello scrollo 
		int y = (GraphicView.dimMappa - c.getY())*GraphicView.dimCasella - (scrollo.getHeight())/2; //posY del dino in pixel con origine in alto a dx
		scrollo.getVerticalScrollBar().setValue(y);
	    scrollo.getHorizontalScrollBar().setValue(x);
		repaint();
		validate();
	}
	public void resetVisibilitaMappa() {
		if (mappa == null) return;
		mappa.resetVisibilita();
	}
}